// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package edu.uoc.tfc.service;

import java.io.ByteArrayOutputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.ResourceBundle;

import javax.activation.DataHandler;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.codec.Base64;
import com.itextpdf.text.pdf.draw.VerticalPositionMark;

import edu.uoc.tfc.dbmodel.Factura;
import edu.uoc.tfc.dbmodel.Part;
import edu.uoc.tfc.dto.AsseguradoraVO;
import edu.uoc.tfc.dto.ClientVO;
import edu.uoc.tfc.dto.CriteriaAsseguradoraVO;
import edu.uoc.tfc.dto.CriteriaClientVO;
import edu.uoc.tfc.dto.CriteriaPartVO;
import edu.uoc.tfc.dto.CriteriaTipusSinistreVO;
import edu.uoc.tfc.dto.FacturaVO;
import edu.uoc.tfc.dto.PartVO;
import edu.uoc.tfc.dto.ResultatCercaPartVO;
import edu.uoc.tfc.dto.TipusSinistreVO;
import edu.uoc.tfc.web.DownloadWrapperVO;
import edu.uoc.tfc.web.EmktDataSource;

/**
 * @see edu.uoc.tfc.service.PartService
 */
public class PartServiceImpl
    extends edu.uoc.tfc.service.PartServiceBase
{

	private ResourceBundle resourceBundle = ResourceBundle.getBundle("application-resources");
	
	private  BaseFont bf;
	private  Font fontHelvetica9 		= new Font(bf, 9);
	private  Font fontHelvetica9Bold 	= new Font(bf, 9);

	@Override
	protected PartVO handleGetPart(String partId) throws Exception {
		
		Part part = getPartDao().load(Long.valueOf(partId));
		
		PartVO partVO = this.getPartDao().toPartVO(part);
		partVO.setAsseguradora(this.getAsseguradoraDao().toAsseguradoraVO(part.getAsseguradora()));
		partVO.setClient(this.getClientDao().toClientVO(part.getClient()));
		partVO.setTipusSinistre(this.getTipusSiniestreDao().toTipusSinistreVO(part.getTipusSinistre()));
		
		if(!part.getFacturaDelPart().isEmpty()){
			Iterator<Factura> it = part.getFacturaDelPart().iterator();
			Factura f = it.next();
			partVO.setFacturaDelPart(this.getFacturaDao().toFacturaVO(f));
		}
		return partVO;
	}

	@Override
	protected ResultatCercaPartVO handleCercaParts(CriteriaPartVO criteriaPart)
			throws Exception {
		
		@SuppressWarnings("unchecked")
		Collection<PartVO> parts = getPartDao().findPartsByCriteria(
				this.getPartDao().TRANSFORM_PARTVO,
				criteriaPart);
		
		ResultatCercaPartVO resultat = new ResultatCercaPartVO();
		
		resultat.setElements(parts);
		resultat.setTotalRecords(parts.size());		
		return resultat;       	
	}

	@Override
	protected void handleAltaPart(PartVO part, String asseguradoraId,
			String tipusSinistreId, String clientId) throws Exception {
		
		Part p = Part.Factory.newInstance(
				getPartNumberFromDate(), 
				part.getPolissa(), 
				part.getDataSinistre(), 
				part.getBaseImposable(),
				part.getPercentatgeIva(),
				part.getObservacions(),
				true,
				getTipusSiniestreDao().load(Long.valueOf(tipusSinistreId)),
				null,
				getClientDao().load(Long.valueOf(clientId)),
				getAsseguradoraDao().load(Long.valueOf(asseguradoraId)));
		
		getPartDao().create(p);
	}

	@Override
	protected void handleModificaPart(PartVO part, String asseguradoraId,
			String tipusSinistreId, String clientId) throws Exception {
		
		part.setObert(part.getObert()==null?false:part.getObert());
		part.setTipusSinistre((TipusSinistreVO)getTipusSiniestreDao().
				load(getTipusSiniestreDao().TRANSFORM_TIPUSSINISTREVO, Long.valueOf(tipusSinistreId)));
		
		part.setClient((ClientVO)getClientDao()
				.load(getClientDao().TRANSFORM_CLIENTVO, Long.valueOf(clientId)));
		
		part.setAsseguradora((AsseguradoraVO)getAsseguradoraDao()
				.load(getAsseguradoraDao().TRANSFORM_ASSEGURADORAVO, Long.valueOf(asseguradoraId)));
		
		getPartDao().update(getPartDao().partVOToEntity(part));		
		
	}

	@Override
	protected void handleTancarPart(PartVO part) throws Exception {
		Part p = getPartDao().load(Long.valueOf(part.getId()));
		p.setObert(Boolean.FALSE);
		getPartDao().update(p);
		
		Factura f = Factura.Factory.newInstance(
				part.getCodiPart() + "F", getDateInText(), 
				getPartDao().load(Long.valueOf(part.getId())));
		
		getFacturaDao().create(f);
	}

	@Override
	protected DownloadWrapperVO handleImprimirPart(String partId) throws Exception {
		Part parte = getPartDao().load(Long.valueOf(partId));
		PartVO partVO = this.getPartDao().toPartVO(parte);
		partVO.setAsseguradora(this.getAsseguradoraDao().toAsseguradoraVO(parte.getAsseguradora()));
		partVO.setClient(this.getClientDao().toClientVO(parte.getClient()));
		partVO.setTipusSinistre(this.getTipusSiniestreDao().toTipusSinistreVO(parte.getTipusSinistre()));

		Document doc = new Document();
		ByteArrayOutputStream ba = new ByteArrayOutputStream();
		PdfWriter writer = PdfWriter.getInstance(doc, ba);	 
		Paragraph pBlanc = new Paragraph("  ");
		doc.open();
		doc.newPage();
		doc.add(pBlanc);
		doc.add(pBlanc);
		doc.add(getLogoTable());
		doc.add(pBlanc);
		doc.add(pBlanc);
		doc.add(pBlanc);
		doc.add(pBlanc);
		doc.add(getDadesDelClient(partVO.getClient()));
		doc.add(pBlanc);
		doc.add(getDadesDelPart(partVO));
		doc.close();
		ba.close();
		
		EmktDataSource ds = new EmktDataSource(ba.toByteArray(),"application/pdf");		
		DataHandler fileData = new DataHandler(ds);
		DownloadWrapperVO dwVO = new DownloadWrapperVO(fileData, parte.getCodiPart() + ".pdf");
		return dwVO;	
	}
	
	private String getPartNumberFromDate(){
		return "P-" + String.valueOf(Calendar.getInstance().getTimeInMillis());
	}
	
	private String getDateInText(){
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			return sdf.format(new Date());			
		}catch (Exception e){
			return "--";
		}
	}

	@Override
	protected TipusSinistreVO[] handleCarregaTipusSinistre() throws Exception {
		
		CriteriaTipusSinistreVO crit= new CriteriaTipusSinistreVO();
		crit.setActiu(Boolean.TRUE);
		
		@SuppressWarnings("unchecked")
		Collection<TipusSinistreVO> tipus = 
				getTipusSiniestreDao().findTipusSinistreByCriteria(
						getTipusSiniestreDao().TRANSFORM_TIPUSSINISTREVO,
						crit);
		
		TipusSinistreVO[] arrReturn = new TipusSinistreVO[tipus.size()];
		int i=0;
		for(TipusSinistreVO t:tipus){
			arrReturn[i]=t;
			i++;
		}
		return arrReturn;	
	}

	@Override
	protected AsseguradoraVO[] handleCarregaAsseguradores() throws Exception {
		CriteriaAsseguradoraVO crit= new CriteriaAsseguradoraVO();
		crit.setActiva(Boolean.TRUE);
		
		@SuppressWarnings("unchecked")
		Collection<AsseguradoraVO> asseguradores = 
				getAsseguradoraDao().findAsseguradoresByCriteria(
						getAsseguradoraDao().TRANSFORM_ASSEGURADORAVO,
						crit);
		
		AsseguradoraVO[] arrReturn = new AsseguradoraVO[asseguradores.size()];
		int i=0;
		for(AsseguradoraVO a:asseguradores){
			arrReturn[i]=a;
			i++;
		}
		return arrReturn;	
	}

	@Override
	protected ClientVO[] handleCarregaClients() throws Exception {
		CriteriaClientVO crit= new CriteriaClientVO();
		crit.setActiu(Boolean.TRUE);
		
		@SuppressWarnings("unchecked")
		Collection<ClientVO> clients = 
				getClientDao().findClientsByCriteria(
						getClientDao().TRANSFORM_CLIENTVO,
						crit);
		
		ClientVO[] arrReturn = new ClientVO[clients.size()];
		int i=0;
		for(ClientVO c:clients){
			arrReturn[i]=c;
			i++;
		}
		return arrReturn;
	}

	@Override
	protected FacturaVO handleGetFactura(String facturaId) throws Exception {
		Part part = getPartDao().load(Long.valueOf(facturaId));
		Collection<Factura> facs = part.getFacturaDelPart();
		Iterator<Factura> it = part.getFacturaDelPart().iterator();
		if (it.hasNext()){
			Factura f = it.next();
			FacturaVO fVO = this.getFacturaDao().toFacturaVO(f);
			fVO.setPartDeLaFactura(getPartDao().toPartVO(part));
			return fVO;
		}else{
			return null;
		}
		
		
	}

	@Override
	protected DownloadWrapperVO handleImprimirFactura(String partId) throws Exception {
				
		Part part = getPartDao().load(Long.valueOf(partId));
		PartVO partVO = this.getPartDao().toPartVO(part);
		partVO.setAsseguradora(this.getAsseguradoraDao().toAsseguradoraVO(part.getAsseguradora()));
		partVO.setClient(this.getClientDao().toClientVO(part.getClient()));
		partVO.setTipusSinistre(this.getTipusSiniestreDao().toTipusSinistreVO(part.getTipusSinistre()));

		String codiFac = "";
		String dataFac = "";
		Collection<Factura> facs = part.getFacturaDelPart();
		Iterator<Factura> it = part.getFacturaDelPart().iterator();
		if (it.hasNext()){
			Factura f = it.next();
			codiFac = f.getCodiFactura();
			dataFac = f.getDataEmissio();
		}
		
		Document doc = new Document();
		ByteArrayOutputStream ba = new ByteArrayOutputStream();
		PdfWriter writer = PdfWriter.getInstance(doc, ba);	 
		Paragraph pBlanc = new Paragraph("  ");
		doc.open();
		doc.newPage();
		doc.add(pBlanc);
		doc.add(pBlanc);
		doc.add(getLogoTable());
		doc.add(pBlanc);
		doc.add(pBlanc);
		doc.add(pBlanc);
		doc.add(pBlanc);
		doc.add(getDadesDelClient(partVO.getClient()));
		doc.add(pBlanc);
		doc.add(getDadesDeFactura(codiFac, dataFac));
		doc.add(pBlanc);
		doc.add(getDadesDelPart(partVO));
		doc.close();
		ba.close();

		EmktDataSource ds = new EmktDataSource(ba.toByteArray(),"application/pdf");		
		DataHandler fileData = new DataHandler(ds);
		DownloadWrapperVO dwVO = new DownloadWrapperVO(fileData, codiFac + ".pdf");
		return dwVO;	
		
	}
	
	private PdfPTable getLogoTable() throws Exception{
		
		//Logo de la UOC en B64
		String imgB64 = "iVBORw0KGgoAAAANSUhEUgAAASgAAAA1CAIAAAB9WuG/AAAJd0lEQVR42u1cv2+cRRDNP0RHE5pQBYpAQxoooksTBQoXCV0QoXQkqIIj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= Image.getInstance(Base64.decode(imgB64)); 
	    PdfPTable logoTable = new PdfPTable(1);
	    logoTable.setWidthPercentage(50F);
	    logoTable.setHorizontalAlignment(Element.ALIGN_RIGHT);
	    
	    PdfPCell cell = new PdfPCell();
	    cell.setBorder(Rectangle.NO_BORDER);
	    cell.addElement(img);
	    
	    logoTable.addCell(cell);
	    return logoTable;
	}
	
	
	private PdfPTable getDadesDelPart(PartVO p) throws Exception{
		int[] header_widths = {20,40,20,20};
		
		PdfPTable theTable = new PdfPTable(1);
		theTable.setWidthPercentage(100F);
		theTable.setSpacingAfter(0F);
		theTable.setSpacingBefore(0F);
	
		PdfPTable theContent = new PdfPTable(4); 
		theContent.setWidths(header_widths);    	
		theContent.setWidthPercentage(100F);
		
	    PdfPCell cHeader = new PdfPCell(new Phrase(resourceBundle.getString("pdf.part.titolSinistre"), 
											fontHelvetica9Bold));	    
	    cHeader.setFixedHeight(20F);
	    cHeader.setColspan(4);
	    cHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
	    theContent.addCell(cHeader);
	
		
	    PdfPCell cLabelCodi = new PdfPCell(new Phrase(resourceBundle.getString("pdf.part.codiPart"),
				  							fontHelvetica9Bold));	    
	    cLabelCodi.setFixedHeight(30F);
	    PdfPCell cValueCodi = new PdfPCell(new Phrase(p.getCodiPart(),
											fontHelvetica9));
	    cValueCodi.setFixedHeight(30F);
	    cValueCodi.setColspan(3);
	    theContent.addCell(cLabelCodi);
	    theContent.addCell(cValueCodi);
	    
	    PdfPCell cLabelTipus = new PdfPCell(new Phrase(resourceBundle.getString("pdf.part.tipusSinistre"),
					fontHelvetica9Bold));	    
		cLabelTipus.setFixedHeight(30F);
		PdfPCell cValueTipus = new PdfPCell(new Phrase(p.getTipusSinistre().getNom(),
						fontHelvetica9));	    
		cValueTipus.setFixedHeight(30F);
		cValueTipus.setColspan(3);
		theContent.addCell(cLabelTipus);
		theContent.addCell(cValueTipus);
	
	    
	    PdfPCell cLabelAsseguradora = new PdfPCell(new Phrase(resourceBundle.getString("pdf.part.asseguradora"),
					fontHelvetica9Bold));	    
	    cLabelAsseguradora.setFixedHeight(30F);	 	    
		PdfPCell cValueAsseguradora = new PdfPCell(new Phrase(p.getAsseguradora().getNom(),
						fontHelvetica9));	    
		cValueAsseguradora.setFixedHeight(30F);	 
		cValueAsseguradora.setColspan(3);
	    theContent.addCell(cLabelAsseguradora);
	    theContent.addCell(cValueAsseguradora);
		
	    PdfPCell cLabelData = new PdfPCell(new Phrase(resourceBundle.getString("pdf.part.dataSinistre"),
					fontHelvetica9Bold));	    
	    cLabelData.setFixedHeight(30F);
		PdfPCell cValueData = new PdfPCell(new Phrase(p.getDataSinistre(),
				fontHelvetica9));	    
		cValueData.setFixedHeight(30F);	 	
	    theContent.addCell(cLabelData);
	    theContent.addCell(cValueData);
	    
	    PdfPCell cLabelPolissa = new PdfPCell(new Phrase(resourceBundle.getString("pdf.part.polissa"),
					fontHelvetica9Bold));	    
	    cLabelPolissa.setFixedHeight(30F);
		PdfPCell cValuePolissa = new PdfPCell(new Phrase(p.getPolissa(),
				fontHelvetica9));	    
		cValuePolissa.setFixedHeight(30F);	 	
	    theContent.addCell(cLabelPolissa);
	    theContent.addCell(cValuePolissa);
	    
	    PdfPCell cLabelImport = new PdfPCell(new Phrase(resourceBundle.getString("pdf.part.import"),
					fontHelvetica9Bold));
	    cLabelImport.setFixedHeight(30F);

	    
	    Double total = p.getBaseImposable() + ((p.getBaseImposable()*p.getPercentatgeIva())/100);
	    DecimalFormat twoDForm = new DecimalFormat("#.##");
		
	    Chunk tab = new Chunk(new VerticalPositionMark(), 100); 
	    Phrase textImport = new Phrase(resourceBundle.getString("pdf.part.base"),fontHelvetica9);
	    textImport.add(tab);
	    textImport.add(p.getBaseImposable().toString() + resourceBundle.getString("pdf.part.euros"));
	    textImport.add("\n");
	    textImport.add(resourceBundle.getString("pdf.part.iva"));
	    textImport.add(tab);
	    textImport.add(tab);
	    textImport.add(p.getPercentatgeIva().toString());
	    textImport.add("\n\n");
	    textImport.add(resourceBundle.getString("pdf.part.total"));
	    textImport.add(tab);
	    textImport.add(tab);
	    textImport.add(twoDForm.format(total).toString() + resourceBundle.getString("pdf.part.euros"));
	    
		PdfPCell cValueImport = new PdfPCell(textImport);		
		cValueImport.setFixedHeight(60F);
		cValueImport.setColspan(3);
	    theContent.addCell(cLabelImport);
	    theContent.addCell(cValueImport);
	    
	    
	    PdfPCell cLabelObservacions = new PdfPCell(new Phrase(resourceBundle.getString("pdf.part.comentaris"),
					fontHelvetica9Bold));
	    cLabelObservacions.setFixedHeight(30F);
	    
		PdfPCell cValueObservacions = new PdfPCell(new Phrase(p.getObservacions(),
				fontHelvetica9));		
		cValueObservacions.setFixedHeight(50F);
		cValueObservacions.setColspan(3);
	    theContent.addCell(cLabelObservacions);
	    theContent.addCell(cValueObservacions);	    
	    
	    theTable.addCell(theContent);	    	    
		return theTable;
	}

	private PdfPTable getDadesDeFactura(String codiFac, String dataFac) throws Exception{
		int[] header_widths = {15,35,15,35};
		
		PdfPTable theTable = new PdfPTable(1);
		theTable.setWidthPercentage(100F);
		theTable.setSpacingAfter(0F);
		theTable.setSpacingBefore(0F);
	
		PdfPTable theContent = new PdfPTable(4); 
		theContent.setWidths(header_widths);    	
		theContent.setWidthPercentage(100F);
		
	    PdfPCell cHeader = new PdfPCell(new Phrase(resourceBundle.getString("pdf.factura.titol"), 
											fontHelvetica9Bold));	    
	    cHeader.setFixedHeight(20F);
	    cHeader.setColspan(4);
	    cHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
	    theContent.addCell(cHeader);
			
	    PdfPCell cLabelCodi = new PdfPCell(new Phrase(resourceBundle.getString("pdf.factura.codi"),
				  							fontHelvetica9Bold));	    
	    cLabelCodi.setFixedHeight(30F);
	    PdfPCell cValueCodi = new PdfPCell(new Phrase(codiFac,
											fontHelvetica9));
	    cValueCodi.setFixedHeight(30F);
	    theContent.addCell(cLabelCodi);
	    theContent.addCell(cValueCodi);
	    
	    PdfPCell cLabelTipus = new PdfPCell(new Phrase(resourceBundle.getString("pdf.factura.data"),
					fontHelvetica9Bold));	    
		cLabelTipus.setFixedHeight(30F);
		PdfPCell cValueTipus = new PdfPCell(new Phrase(dataFac,
						fontHelvetica9));	    
		cValueTipus.setFixedHeight(30F);
		
		theContent.addCell(cLabelTipus);
		theContent.addCell(cValueTipus);
	
	    
	    theTable.addCell(theContent);	    	    
		return theTable;
	}

	
	private PdfPTable getDadesDelClient(ClientVO client) throws Exception{
		
		
		int[] header_widths = {10,45,25,10};
		
		PdfPTable theTable = new PdfPTable(1);
		theTable.setWidthPercentage(100F);
		theTable.setSpacingAfter(0F);
		theTable.setSpacingBefore(0F);
	
		PdfPTable theContent = new PdfPTable(4); 
		theContent.setWidths(header_widths);    	
		theContent.setWidthPercentage(100F);

	    PdfPCell cHeader = new PdfPCell(new Phrase(resourceBundle.getString("pdf.part.titolClient"), 
											fontHelvetica9Bold));	    
	    cHeader.setFixedHeight(20F);
	    cHeader.setColspan(4);
	    cHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
	
		
	    PdfPCell cDni = new PdfPCell(new Phrase(resourceBundle.getString("pdf.part.dni"), 
				  							fontHelvetica9Bold));	    
	    cDni.setFixedHeight(20F);	 	    
	    PdfPCell cDniValue = new PdfPCell(new Phrase(client.getDni(), 
											fontHelvetica9));	    
	    cDniValue.setFixedHeight(20F);
	
	    PdfPCell cCognoms = new PdfPCell(new Phrase(resourceBundle.getString("pdf.part.cognoms"), 
					fontHelvetica9Bold));	    
	    cCognoms.setFixedHeight(20F);	 	    
		PdfPCell cCognomsValue = new PdfPCell(new Phrase(client.getPrimerCognom() + " " + client.getSegonCognom(), 
											fontHelvetica9));	    
		cCognomsValue.setFixedHeight(20F);
	    
	    PdfPCell cNom = new PdfPCell(new Phrase(resourceBundle.getString("pdf.part.nom"), 
					fontHelvetica9Bold));	    
	    cNom.setFixedHeight(20F);	 	    
		PdfPCell cNomValue = new PdfPCell(new Phrase(client.getNom(), 
											fontHelvetica9));	    
		cNomValue.setFixedHeight(20F);
	
	    PdfPCell cTelefon = new PdfPCell(new Phrase(resourceBundle.getString("pdf.part.telefon"), 
					fontHelvetica9Bold));	    
	    cTelefon.setFixedHeight(20F);	 	    
		PdfPCell cTelefonValue = new PdfPCell(new Phrase(client.getTelefon(), 
											fontHelvetica9));	    
		cTelefonValue.setFixedHeight(20F);
	
		
	    theContent.addCell(cHeader);
	    theContent.addCell(cDni);
	    theContent.addCell(cCognoms);
	    theContent.addCell(cNom);
	    theContent.addCell(cTelefon);
	    theContent.addCell(cDniValue);
	    theContent.addCell(cCognomsValue);
	    theContent.addCell(cNomValue);
	    theContent.addCell(cTelefonValue);
	
	    theTable.addCell(theContent);	    	    
		return theTable;
	}

}